<?php
/**
 * Created by jinqiang on 2024/12/31 13:19.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Repository;


class BanniuRepository
{
    const SECRET = "b51f121477f25838f480250e6c6478a5";

    const TOKEN = "2beeb427c6ec13ceaa7c34345de5d8eb";

    const APP_KEY = "3325326110";

    public static function list($data, $page, $limit)
    {
        $param = [
            "format"         => 'json',
            "app_key"        => self::APP_KEY,
            "sign_method"    => 'md5',
            "access_token"     => self::TOKEN,
            "v"     => '1'
        ];
        $param['task_status'] = 0; //工单状态：0、待处理；1、已完成；2、处理中；3、暂停中
        if($data['star_modified'] ?? "" && $data['end_modified'] ?? ""){//修改时间
            $param['star_modified'] = $data['star_modified'];
            $param['end_modified'] = $data['end_modified'];
        }
        if($data['sourceId'] ?? ""){
            $param['condition_column'] = json_encode([
                ["id" => 819,"behaviorType" => 1, "searchType" => "5","value" => str_replace(",", "\n", $data['sourceId'])]
            ]);
        }
        $param['project_id'] = "790";
        $param['page_size'] = $limit;
        $param['page_num'] = $page;
        $param['method']    = "task.list";                                           // 此处填写要应对应BODY，
        $param['timestamp'] = date("Y-m-d H:i:s");                               //时间
        $param['sign']      = self::sign($param);   //利用body签名
        $url = "https://open.bytenew.com/gateway/api/miniAPI?".http_build_query($param);
        $return = httpCurl($url, [], "get");
        return json_decode($return, true);
    }

    public static function quickUpdate($taskId)
    {
        $param = [
            "format"         => 'json',
            "app_key"        => self::APP_KEY,
            "sign_method"    => 'md5',
            "access_token"     => self::TOKEN,
            "v"     => '1'
        ];
        $jsonData = json_encode([
            "contents" => json_encode([
                [
                    "project_id" => '790',
                    "task_id" => $taskId,
                    "app_id" => '22320',
                    "task_status" => 1,
                ]
            ])
        ]);
        $form = [
            "json" => $jsonData,
            "headers" => ['Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($jsonData)]
        ];
        $param['method']    = "task.quickUpdate";
        $param['timestamp'] = date("Y-m-d H:i:s");
        $param['seller_id'] = "262344251301";
        $param['sign']      = self::sign($param);
        $url = "https://open.bytenew.com/gateway/api/miniAPI?".http_build_query($param);
        $res = httpCurl($url, $form);
        return json_decode($res, true);
    }

    //签名
    public static function sign($param)
    {
        ksort($param);
        $outputStr = '';
        foreach ($param as $k => $v) {
            if ( strlen($v) == '' ) {
                exit('Param can\'t error!');
            }
            $outputStr .= $k . $v;
        }
        $outputStr = self::SECRET . $outputStr . self::SECRET;
        return strtoupper(md5($outputStr));
    }
}
